/*入口函数*/
$(function () {
    Bmob.initialize("d5e6671ae5ce13f4", "24166")
    Bmob.debug(true)
    //alert("如果你打开页面看到{{xxx}}之类的内容，那么就是获取数据出错了，建议使用其他的浏览器打开")
    let msg = new Array()
    let vmList
    //弹窗
    getNotice()
    new WOW().init();
    chart()
    $("#mdb-lightbox-ui").load("mdb-addons/mdb-lightbox-ui.html");
    updateViewCount()
    getMyDiary()
    showMsgList()
    getMsgList()
    getCommit()
    getViewCount()
    getMusicList()


    /*滚动条事件*/
    $(document).scroll(function () {
        let nowPosition = $(this).scrollTop();
    });

    //配置图表数据
    function chart() {
        // 基于准备好的dom，初始化echarts实例
        var myChart = echarts.init(document.getElementById('main'));
        var myChart2 = echarts.init(document.getElementById('main2'));

        // 指定图表的配置项和数据
        var programChart = {
            title: {
                text: '编程语言'
            },
            tooltip: {},
            legend: {
                //data:['销量']
            },
            xAxis: {
                data: ["Java", "Html5+Css3", "kotlin", "SQL", "JavaScript"]
            },
            yAxis: {},
            series: [{
                //name: '销量',
                type: 'bar',
                data: [36, 28, 20, 20, 20]
            }]
        };

        // 指定图表的配置项和数据
        var frameUtil = {
            title: {
                text: '开发框架'
            },
            tooltip: {},
            legend: {
                //data:['销量']
            },
            xAxis: {
                data: ["Bootstrap", "Vue", "jQuery", "Bmob", "ECharts"]
            },
            yAxis: {},
            series: [{
                //name: '销量',
                type: 'bar',
                data: [25, 24, 23, 22, 21]
            }]
        };

        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(programChart);
        myChart2.setOption(frameUtil);
    }

    //获取音乐列表
    function getMusicList(){
        let query = Bmob.Query("music");
        query.find().then(res => {
            console.log(res)
        });
    }

    //获取通知
    function getNotice() {
        let query = Bmob.Query('web_notic');
        query.find().then(res => {
            $("#notice-title").text(res[0].title)
            $("#notice-content").text(res[0].content)
            if (res[0].is_show) {
                $('#btn_notice').trigger('click')
            }

        });
    }
    // 获取日记
    function getMyDiary() {
        let diaryList = new Array()
        let query = Bmob.Query('diary_table');
        query.find().then(res => {
            console.log(res)
            let index = 0;
            for (let obj of res) {
                diaryList[index] = obj
                index += 1
            }
            new Vue({
                el: '#my-diary',
                data: {
                    sites: diaryList
                },
                methods: {
                    pressThumb: function (id, num, obj) {//点赞
                        query = Bmob.Query('diary_table');
                        query.get(id).then(res => {
                            console.log(res)
                            res.set('thumb', ++num)
                            res.save()
                            obj.thumb++
                            showTopMsg("点赞成功，谢谢!")
                        }).catch(err => {
                            console.log(err)
                            showTopMsg("点赞失败，要不再试试？")
                        })
                    }
                }
            })
        });
    }

    // 获取访问量
    function getViewCount() {
        query = Bmob.Query("view_count");
        query.count().then(res => {
            let vm=new Vue({
                el: "#text-view-count",
                data: {
                    number:0,
                    number2:0,
                    prefix: "网站访问量："
                },
                computed:{
                    newNum:function () { 
                        return this.number2.toFixed(0)
                     }
                },
                watch:{
                    number:function(newNumber){
                        gsap.to(this.$data, { duration: 1, number2: newNumber })
                        //console.log("数值发生变化："+newNumber)
                    }
                }
            })
            vm.$data.number=res
        })
    }
    /* 更新问量*/
    function updateViewCount() {
        let userIp = JSON.stringify(returnCitySN)
        let query = Bmob.Query('view_count');
        query.set("name", userIp)
        query.save().then(res => {
            console.log("访问量+1，目前" + res)
        }).catch(err => {
            console.log(err)
        })
    }
    // 显示顶部通知
    function showTopMsg(content) {
        $("#msg-content").text(content)
        $('#show-msg').trigger('click')
    }

    //显示评论列表
    function showMsgList() {
        vmList = new Vue({
            el: '#speak-container',
            data: {
                sites: msg
            }
        })
    }
    // 获取评论列表
    function getMsgList() {
        let query = Bmob.Query('speak');
        query.find().then(res => {
            console.log(res)
            let index = 0;
            for (let obj of res) {
                //msg[index]=obj
                vmList.$set(vmList.sites, index, obj)
                index += 1
            }
        })
    }
    // 获取评论内容
    function getCommit() {
        $("#post-content").click(function () {
            let name = $("#textarea_name").val()
            if (name == "") name = "匿名网友"
            let content = $("#textarea_msg").val()
            if (content == "") {
                showTopMsg("需要填写完整的内容才能提交！")
            } else {
                commit(name, content)
            }

        });
    }
    // 发送评论
    function commit(name, content) {
        let userIp = JSON.stringify(returnCitySN)
        let query = Bmob.Query('speak');
        query.set("name", name)
        query.set("message", content)
        query.set("ip", returnCitySN.cip)
        query.set("province", returnCitySN.cname)
        query.set("city_cn", userIp)
        query.save().then(res => {
            showTopMsg("发送成功")
            //refresh()
            //showMsgList()
            getMsgList()
        }).catch(err => {
            showTopMsg("发送失败：" + err)
        })
    }
    // 刷新页面
    function refresh() {
        window.location.reload()
    }
    //检测页面状态
    function checkPage() {

    }
})